<?php
    /*
        移动端宽为 640 像素
        $_REQUEST[code] 的 code 为微信专用，别再使用了
    */
    session_start();
    date_default_timezone_set('PRC');                   //+8 时区
    ini_set("error_reporting","E_ALL & ~E_NOTICE");     //仅显示错误级别的提示
    header("Content-type: text/html; charset=utf-8");   //UTF8 编码
    //链接数据库
    mysql_pconnect("127.0.0.1","qgz","qgz");
    if(!mysql_select_db("qgz")){ 
        echo mysql_error();
        die("Database error!"); }
    mysql_set_charset("utf8");
    //过滤传入数据
    replace_REQUEST(array("\\"=>"\\\\","'"=>"\'"));
    //引用类 MVC 架构
    $_c=($_REQUEST[_c]?$_REQUEST[_c]:"index");
    $_f=($_REQUEST[_f]?$_REQUEST[_f]:"index");
    include_once("m/$_c.php");
    include_once("c/$_c.php");
    if(is_callable($_f."Action")){
    call_user_func($_f."Action");
}else{
    @header("http/1.1 404 not found");
@header("status: 404 not found");
include("v/m/ad/404.php");
}


    //输出 v 页面////////////////////////////////////////////////////////////////////////////////////////////////
    function out_m($vars=null,$title=""){ global $_c,$_f;
        foreach($vars as $key=>$val){ $$key=$val; }//接收传递的变量
        echo(html_compress("
            <html>
                <head>
                    <meta name='viewport' content='width=640,user-scalable=no, target-densitydpi=device-dpi' />
                    <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
                    <meta name='keywords' content='广州知越信息科技有限公司开发' />
                    <title>$title</title>
                    <link href='v/m/images/style.css?id=3' rel='stylesheet' />
                    <script src='v/m/images/script.js?id=3'></script>
                </head>
                <body>
                "));
        include_once("v/m/$_c/$_f.php");
        echo(html_compress("
                </body>
            </html>
        "));
    }
    function out_w($vars=null,$title=""){ global $_c,$_f;
        foreach($vars as $key=>$val){ $$key=$val; }//接收传递的变量
        echo(html_compress("
            <!DOCTYPE html>
            <html>
                <head>
                    <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
                    <meta name='keywords' content='广州知越信息科技有限公司开发' />
                    <title>$title</title>
                    <link href='v/w/images/style.css?id=3' rel='stylesheet' />
                    <script src='v/w/images/script.js?id=3'></script>
                </head>
                <body>
                "));
        include_once("v/w/$_c/$_f.php");
        echo(html_compress("
                </body>
            </html>
        "));
    }
    //公用函数////////////////////////////////////////////////////////////////////////////////////////////////
    function now($timestamp=null){ if($timestamp){ return date("Y-m-d H:i:s",$timestamp); }else{ return date("Y-m-d H:i:s"); }}
    function html_compress($html){
        //去掉多余空格与换行符 - 顺序别乱
        while(strpos($html,"  ")){ $html=str_replace("  ", " ", $html); }
        $html=str_replace("\n ", "\n", $html);
        while(strpos($html,"\n\n")){ $html=str_replace("\n\n", "\n", $html); }
        $html=str_replace(">\n<", "><", $html);
        return $html;
    }
    function os_device_type(){
        $u = $_SERVER[HTTP_USER_AGENT];
        if(strpos($u,"Pad")>0){ $ret = "Pad";
        }else if(strpos($u,"iPod")>0 || strpos($u,"iPhone")>0 || strpos($u,"Android")>0 || strpos($u,"Mobile")>0){  $ret = "Phone";
        }else{                  $ret = "PC"; }
        if($_REQUEST[_m]){ $ret="_m"; }
        return $ret;
    }
    function os_get_ip(){
        if($_SERVER){ if($_SERVER[HTTP_X_FORWARDED_FOR]){ $realip = $_SERVER[HTTP_X_FORWARDED_FOR]; }elseif($_SERVER[HTTP_CLIENT_IP]){ $realip = $_SERVER[HTTP_CLIENT_IP]; }else{ $realip = $_SERVER[REMOTE_ADDR]; } }else{ if(getenv('HTTP_X_FORWARDED_FOR')){ $realip = getenv('HTTP_X_FORWARDED_FOR'); }elseif(getenv('HTTP_CLIENT_IP')){ $realip = getenv('HTTP_CLIENT_IP'); }else{ $realip = getenv('REMOTE_ADDR'); } }
        return $realip;
    }
    function os_get_domain(){ return strtoupper($_SERVER["HTTP_HOST"]); }
    function replace_REQUEST($rule=null,$and_trim=true,$toupper="cid,uid,id"){
        foreach($_REQUEST as $key=>$val){
            if($key!="PHPSESSID" && substr($key,0,10)!="unlimited_"){
                //应用替换规则
                foreach($rule as $rk=>$rv){
                    if($rv!=null){ $val=str_replace($rk,$rv,$val); }//使用 null 来取消限制
                }
                //大写 及 前后去空
                if(strpos(",$toupper,",",$key,")){ $val=strtoupper($val); }
                if($and_trim){ $val=trim($val); }
                //更新
                $_REQUEST[$key]=$val;
            }
        }
    }
    function saveUploadedFile($fileInput,$fileName,$andResizeToWidth=640){
        if(substr($fileName,0,7)!="upload/"){ $fileName="upload/$fileName"; }
        if(file_exists($fileName)){ unlink($fileName); }
        move_uploaded_file($_FILES[$fileInput]['tmp_name'],$fileName);
        if(file_exists($fileName)){
            chmod($fileName, 0666);
            //按比例缩放
$andResizeToWidth=0;
            if($andResizeToWidth>0){
                $imagedata = getimagesize($fileName);
                $oldWidth = $imagedata[0];
                $oldHeight = $imagedata[1];
                $newWidth = $andResizeToWidth;
                $newHeight = $oldHeight * ($newWidth/$oldWidth);
                $image = imagecreatefromjpeg($fileName);
                $thumb = imagecreatetruecolor($newWidth, $newHeight); 
                imagecopyresized ($thumb, $image, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight); 
                imagejpeg($thumb, $fileName); 
                imagedestroy($thumb);
                imagedestroy($image);
            }
        }
        return $fileName;
    }
    function mq($query){return mysql_query($query);}
    function mfa($v){return mysql_fetch_array($v);}
    function mMakeSet($vars){ foreach($vars as $key=>$val){ $s[]="`$key`='$val'"; } return join(",",$s); }